﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;

namespace OperationINI
{
    public class rwini
    {
        /// <summary>
        /// Copyright (C) 2008 lgamoy@163.com
        /// Ini文件访问类(基于Compact Framework 2.0)
        /// 说明：该类是在Window Mobile上使用的，所以为了保证执行高效，丢失了一些ini文件的特性。比如文件中不能有空行，否则读取不到。

        // Fields
        public string path = string.Empty;

        // Methods
        public rwini(string iniFilePath)
        {
            this.path = iniFilePath;
        }
        #region 读取
        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="ApplicationName">节名</param>
        /// <param name="KeyName">键名</param>
        /// <param name="Default">默认值</param>
        /// <param name="FileName">ini文件名</param>
        /// <returns></returns>
        private string GetPrivateProfileString(string ApplicationName, string KeyName, string Default, string FileName)
        {
            string str = string.Format("[{0}]", ApplicationName);
            string str2 = KeyName + "=";
            StreamReader reader = null;
            try
            {
                string str3 = string.Empty;
                bool flag = false;
                reader = new StreamReader(FileName, Encoding.Default);
                str3 = reader.ReadLine();
                for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
                {
                    if (!flag)
                    {
                        if (str3 == str)
                        {
                            flag = true;
                        }
                    }
                    else
                    {
                        if (str3.StartsWith("[") && str3.EndsWith("]"))
                        {
                            return Default;
                        }
                        if (str3.StartsWith(str2))
                        {
                            return str3.Substring(KeyName.Length + 1);
                        }
                    }
                    str3 = reader.ReadLine();
                }
            }
            catch
            {
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return Default;
        }
        #endregion

        #region 写入

        /// <summary>
        /// ini文件写入
        /// </summary>
        /// <param name="ApplicationName">节名</param>
        /// <param name="KeyName">键名</param>
        /// <param name="Value">要写入的值</param>
        /// <param name="FileName">ini文件</param>
        private void WritePrivateProfileString(string ApplicationName, string KeyName, string Value, string FileName)
        {
            string str = string.Format("[{0}]", ApplicationName);
            string str2 = KeyName + "=";
            try
            {
                StringBuilder builder = new StringBuilder();
                bool flag = false;
                bool flag2 = false;
                bool flag3 = true;
                StreamReader reader = new StreamReader(FileName, Encoding.Default);
                string str3 = reader.ReadLine();
                for (str3 = (str3 == null) ? string.Empty : str3.Trim(); str3.Length > 0; str3 = (str3 == null) ? string.Empty : str3.Trim())
                {
                    if (flag3)
                    {
                        if (!flag2)
                        {
                            builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
                            builder.Append(str3);
                            if (str3 == str)
                            {
                                flag2 = true;
                            }
                        }
                        else if (str3.StartsWith("[") && str3.EndsWith("]"))
                        {
                            builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
                            builder.Append(string.Format("\r\n{0}", str3));
                            flag = true;
                            flag3 = false;
                        }
                        else if (str3.StartsWith(str2))
                        {
                            builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
                            flag = true;
                            flag3 = false;
                        }
                        else
                        {
                            builder.Append(string.Format("\r\n{0}", str3));
                        }
                    }
                    else
                    {
                        builder.Append(string.Format("\r\n{0}", str3));
                    }
                    str3 = reader.ReadLine();
                }
                reader.Close();
                if (!flag2)
                {
                    builder.Append((builder.Length == 0) ? string.Empty : "\r\n");
                    builder.Append(string.Format("[{0}]", ApplicationName));
                }
                if (!flag)
                {
                    builder.Append(string.Format("\r\n{0}={1}", KeyName, Value));
                }
                StreamWriter writer = new StreamWriter(FileName, false, Encoding.Default);
                writer.Write(builder);
                writer.Close();
            }
            catch
            {
            }
        }

        #endregion

        public string IniReadValue(string Section, string Key)
        {
            return this.GetPrivateProfileString(Section, Key, string.Empty, this.path);
        }

        public void IniWriteValue(string Section, string Key, string Value)
        {
            this.WritePrivateProfileString(Section, Key, Value, this.path);
        }
    }
}